package com.lst.projectlib.component.whellpic;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lst.projectlib.R;

/**
 * 日期选择对话框
 * 
 * @author ywl
 *
 */
public class ChangeTextSingleDialog extends Dialog implements android.view.View.OnClickListener {

	private Context context;
	private WheelView wvText;

	private View vChangeBirth;
	private View vChangeBirthChild;
	private TextView btnSure;
	private TextView btnCancel;

	private ArrayList<String> arry_years = new ArrayList<String>();
	private CalendarTextAdapter mYearAdapter;

	private String currentYear = "";

	private int maxTextSize = 22;
	private int minTextSize = 16;

	private boolean issetdata = false;

	private String selectYear;

	private OnTextSingleListener onTextSingleListener;
	
	private String title;
	private int screenWidth;

	public ChangeTextSingleDialog(Context context) {
		super(context, R.style.wheelViewDialog);
		this.context = context;
		
		Window win = getWindow();
		win.setGravity(Gravity.BOTTOM);
		
		WindowManager windowManager = win.getWindowManager();
		DisplayMetrics dm = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(dm);
		screenWidth=dm.widthPixels;
		win.getAttributes().dimAmount = 0.3f;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_myinfo_changetext_single);
		wvText = (WheelView) findViewById(R.id.wv_text);

		vChangeBirth = findViewById(R.id.ly_myinfo_changebirth);
		vChangeBirthChild = findViewById(R.id.ly_myinfo_changebirth_child);
		btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
		btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel);
		TextView tv_share_title=(TextView) findViewById(R.id.tv_share_title);

		vChangeBirth.setOnClickListener(this);
		vChangeBirthChild.setOnClickListener(this);
		btnSure.setOnClickListener(this);
		btnCancel.setOnClickListener(this);

		if (!issetdata) {
			dismiss();
			return;
		}
		
		if(!TextUtils.isEmpty(title)){
			tv_share_title.setText(title);
		}
		
		mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize);
		wvText.setVisibleItems(4);
		wvText.setViewAdapter(mYearAdapter);
		wvText.setCurrentItem(setYear(currentYear));


		wvText.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
				selectYear = currentText;
//				setTextviewSize(currentText, mYearAdapter);
				currentYear = currentText;
				setYear(currentYear);
			}
		});

		wvText.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				// TODO Auto-generated method stub
				String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
				setTextviewSize(currentText, mYearAdapter);
			}
		});
		
		vChangeBirthChild.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT));
	}

	public void initYears(String[] listStr) {
		for (int i = 0; i < listStr.length; i++) {
			arry_years.add(listStr[i]);
		}
	}


	private class CalendarTextAdapter extends AbstractWheelTextAdapter {
		ArrayList<String> list;

		protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index) + "";
		}
	}

	public final void setOnTextSingleListener(OnTextSingleListener onTextSingleListener) {
		this.onTextSingleListener = onTextSingleListener;
	}

	@Override
	public void onClick(View v) {

		if (v == btnSure) {
			if (onTextSingleListener != null) {
				onTextSingleListener.onClick(selectYear);
			}
		} else if (v == btnSure) {

		} else if (v == vChangeBirthChild) {
			return;
		} else {
			dismiss();
		}
		dismiss();

	}

	public interface OnTextSingleListener {
		public void onClick(String year);
	}

	/**
	 * 设置字体大小
	 * 
	 * @param curriteItemText
	 * @param adapter
	 */
	public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
		ArrayList<View> arrayList = adapter.getTestViews();
		int size = arrayList.size();
		String currentText;
		for (int i = 0; i < size; i++) {
			TextView textvew = (TextView) arrayList.get(i);
			currentText = textvew.getText().toString();
			if (curriteItemText.equals(currentText)) {
				textvew.setTextSize(maxTextSize);
			} else {
				textvew.setTextSize(minTextSize);
			}
		}
	}

	/**
	 * 设置年月日
	 */
	public void setDate(String[] listStr, String str) {
		selectYear = str;
		issetdata = true;
		initYears(listStr);
		this.currentYear = str;
	}
	
	public void setDialogTitle(String title){
		this.title=title;
	}

	/**
	 * 设置年份
	 */
	public int setYear(String currentYear) {
		int yearIndex = 0;
		for (int i = 0; i < arry_years.size(); i++) {
			if(arry_years.get(i).equals(currentYear)){
				return yearIndex;
			}
			yearIndex++;
		}
		return yearIndex;
	}
	
	public void notifyData(String curStr) {
		selectYear = curStr;
		currentYear = curStr;
		
		int index = setYear(curStr);
		mYearAdapter.setCurrentIndex(index);
		wvText.setCurrentItem(index);
	}
}